package cn.angis.system.service;

import cn.angis.common.constant.Constant;
import cn.angis.db.service.BaseService;
import cn.angis.system.dto.input.UserInput;
import cn.angis.system.dto.output.LoginOutput;
import cn.angis.system.dto.output.UserOutput;
import cn.angis.system.mapper.UserMapper;
import cn.angis.system.model.AngisUser;
import cn.angis.system.model.Role;
import cn.angis.system.model.Userpost;
import cn.angis.system.model.Userrole;
import cn.angis.system.util.AuthUtil;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 包名称：cn.angis.system.service
 * 类名称：SysUserService
 * 类描述：系统用户服务
 * 创建人：@author angis.cn
 * 创建日期： 2022/12/30 17:39
 */
@Component
public class AngisUserService extends BaseService<UserMapper, AngisUser> {

    @Inject
    private UserroleService userroleService;

    @Inject
    private UserpostService userpostService;

    @Inject
    private RoleresService roleresService;

    @Inject
    private RoleService roleService;

    public AngisUser getByUserName(String userName) {
        return baseMapper.createLambdaQuery().andEq(AngisUser::getDelFlag, "0").andEq(AngisUser::getUsername, userName).singleSimple();
    }

    public void refreshUserByUserId(String userId) {
        SaSession session = StpUtil.getSessionByLoginId(userId, false);
        if (session != null) {
            List<String> roleAndResourceByUserId = roleresService.getRoleAndResourceByUserId(userId);
            LoginOutput loginOutput = AuthUtil.getUserBySession(session);
            List<Role> roleList = roleService.getRoleListByUserId(loginOutput.getId());
            loginOutput.setRoles(roleList);
            loginOutput.setResources(roleAndResourceByUserId);
            session.set(Constant.SESSION_USER_KEY, loginOutput);
        }
    }

    public List<String> getPostListByUserId(String userId) {
        return baseMapper.getSQLManager().lambdaQuery(Userpost.class)
                .andEq(Userpost::getUserId, userId)
                .select().stream().map(Userpost::getPostId)
                .collect(Collectors.toList());
    }

    public List<String> getRoleListByUserId(String userId) {
        return baseMapper.getSQLManager().lambdaQuery(Userrole.class)
                .andEq(Userrole::getUserId, userId)
                .select().stream().map(Userrole::getRoleId)
                .collect(Collectors.toList());
    }

//    public AngisUser getByWxId(String wxId) {
//        return baseMapper.getSQLManager().lambdaQuery(AngisUser.class)
//                .andEq(AngisUser::getWxId, wxId)
//                .singleSimple();
//    }
//
//    public void updateByWxId(AngisUser entity) {
//        Query<AngisUser> query = baseMapper.getSQLManager().lambdaQuery(AngisUser.class);
//        query.andEq("wx_id", entity.getWxId());
//        query.updateSelective(entity);
//    }


    public List<UserOutput> selectUserList(UserInput userInput) {
        return baseMapper.selectUserList(userInput);
    }

}
